package com.example.recycleview;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;

import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private Button button1,button3,button4,button5,button6,button7,button9,buttonSend,buttonSend2;
    private   MyService2.MyBinder mybinder;// =(MyService2.MyBinder) service;
    private  ServiceConnection connection;
    private Intent intent;
    private MyReceiver2 myReceiver2;
    private String broadcast="wy.action.huhuhu";
    private NotificationManager notificationManager;
    private NotificationCompat.Builder builder;
    private NotificationChannel channel;
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button1 = findViewById(R.id.button);
        button3=findViewById(R.id.button3);
        button4=findViewById(R.id.button4);
        button5=findViewById(R.id.button5);
        button6=findViewById(R.id.button6);
        button7=findViewById(R.id.button7);
        buttonSend=findViewById(R.id.buttonSend);
        buttonSend2=findViewById(R.id.buttonSend2);
        IntentFilter filter = new IntentFilter();
        filter.addAction(broadcast);
        registerReceiver(myReceiver2,filter);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(broadcast);
                sendBroadcast(intent);
            }
        });
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        channel = null;
        //Android8.0要求设置通知渠道
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel("foreground", "foregroundName",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }
        builder=new NotificationCompat.Builder(MainActivity2.this,"foreground");
        builder.setSmallIcon(R.drawable.ic_launcher_background).setContentTitle("title通知").
                setContentText("这是一个通知").
                setWhen(System.currentTimeMillis());
        buttonSend2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notificationManager.notify(1,builder.build());
            }
        });

        if (ActivityCompat.checkSelfPermission(MainActivity2.this,
                Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.RECEIVE_SMS)
                        != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity2.this,
                    new String[]{Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS},
                    1);
        }//动态申请权限
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent =new Intent(MainActivity2.this,MainActivity3.class);
               intent.putExtra("name","chf");

                //intent.putExtra("age",20);
                //startActivity(intent);
                startActivityForResult(intent,1);

            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(MainActivity2.this,MyService1.class);
                startService(intent);
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //intent = new Intent(MainActivity2.this,MyService1.class);
                stopService(intent);
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              intent = new Intent(MainActivity2.this,MyService2.class);
              intent.setType("MainAcitivity2");
                connection= new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName componentName, IBinder service) {
                         mybinder =(MyService2.MyBinder) service;
                        mybinder.play();

                    }

                    @Override
                    public void onServiceDisconnected(ComponentName componentName) {

                    }
                };
                bindService(intent,connection, Context.BIND_AUTO_CREATE);
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(connection);
            }
        });
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity2.this,MainActivity5.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("life","acitivity2 is onResume");
        //Toast.makeText(this,"acitivity2 is onResume",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("life","acitivity2 is onDestroy");
        //Toast.makeText(this,"acitivity2 is onDestroy",Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("life","acitivity2 is onStart");
        //Toast.makeText(this,"acitivity2 is onStart",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("life","acitivity2 is onRestart");
        //Toast.makeText(this,"acitivity2 is restart",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("life","acitivity2 is onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("life","acitivity2 is onStop");
        //Toast.makeText(this,"acitivity2 is onstop",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==31){
            String name=data.getStringExtra("sta");
            Log.d("life",name);
        }

    }
}